package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

import org.apache.tomcat.jni.Mmap;

import model.Communication;
import model.User;

public class UserData {
	public boolean addUser(User user) {
		boolean result = false;
		Connection connection = DbConnection.getConnect();
		String sql = "INSERT INTO user VALUES(?,?,?,?,?,?,?,?)";
		try {
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setString(1, user.getEmail());
			preparedStatement.setString(2, user.getName());
			preparedStatement.setString(3, user.getSex());
			preparedStatement.setString(4, user.getPhone());
			preparedStatement.setString(5, user.getFile());
			preparedStatement.setString(6, user.getDayOfBirth());
			preparedStatement.setString(7, user.getPasswd());
			preparedStatement.setDouble(8, user.getMoney());
			int i = preparedStatement.executeUpdate();
			if (i > 0) {
				result = true;
			}
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public boolean checkEmail(String email) {
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM user WHERE email = '" + email + "'";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			ResultSet rs = pre.executeQuery();
			if (rs.next())
				return true;
			else
				return false;

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;

	}

	public void addCommunication(String link, long timeContent, String title,
			int type , String user_email) {
		Connection connection = DbConnection.getConnect();
		String sql = "INSERT INTO communication VALUES(?,?,?,?,?)";
		System.out.println(title);
		try {
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);

			preparedStatement.setLong(1, timeContent);
			preparedStatement.setString(2, title);
			preparedStatement.setString(3, link);
			preparedStatement.setInt(4, type);
			preparedStatement.setString(5, user_email);
			preparedStatement.executeUpdate();
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public boolean checkUserName(String email) {

		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM user WHERE email = '" + email + "'";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			ResultSet rs = pre.executeQuery();
			if (rs.next())
				return true;
			else
				return false;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;

	}

	public void updatePass(String passUpdate, String email) {
		Connection con = DbConnection.getConnect();
		String sql = "UPDATE user SET pass = '" + passUpdate
				+ "' WHERE email = '" + email + "'";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			pre.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static double getMoney(String email) {
		double money = 0;
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM user WHERE email = ?";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, email);

			ResultSet rs = pre.executeQuery();
			while (rs.next()) {
				money = rs.getDouble("money");
			}
			rs.close();
			pre.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return money;

	}

	public static void updateMoney(String email, double money) {
		Connection con = DbConnection.getConnect();
		String sql = "UPDATE user SET money =? WHERE email = ?";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			pre.setDouble(1, money);
			pre.setString(2, email);
			pre.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public boolean checkLogin(String email, String pass) {
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM user WHERE email = '" + email
				+ "' AND pass = '" + pass + "'";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			ResultSet rs = pre.executeQuery();
			if (rs.next()) {
				rs.close();
				pre.close();
				return true;
			} else {
				rs.close();
				pre.close();
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean checkIdcard(String idcard, String idSericard) {
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM cardGame WHERE idcard = '" + idcard
				+ "' AND idSericard = '" + idSericard + "'"
				+ " AND checkEnable = '0'";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			ResultSet rs = pre.executeQuery();
			if (rs.next())
				return true;
			else
				return false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;

	}

	public static void EnableCard(String idSericard) {
		Connection con = DbConnection.getConnect();
		String sql = "UPDATE cardGame SET checkEnable =? WHERE idSericard = ?";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			pre.setInt(1, 1);
			pre.setString(2, idSericard);
			pre.executeUpdate();
			pre.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static double getAmountCard(String idSericard) {
		double amount = 0;
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM cardGame WHERE idSericard = ?";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, idSericard);
			ResultSet rs = pre.executeQuery();
			while (rs.next()) {
				amount = rs.getDouble("amount");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return amount;

	}

	public static User getUser(String email) {
		User user = null;
		try {

			Connection connection = DbConnection.getConnect();
			String sql = "select * from user WHERE email = '" + email + "'";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				String passwd = resultSet.getString("pass");
				String name = resultSet.getString("name");
				String sex = resultSet.getString("sex");
				String dayOfBirth = resultSet.getString("dayOfBirth");
				String phone = resultSet.getString("phone");
				String file = resultSet.getString("file");
				double money = resultSet.getDouble("money");
				user = new User(email, passwd, name, sex, dayOfBirth, phone,
						file, money);
			}
			preparedStatement.close();
			connection.close();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return user;
	}

	public void updateAccount(String email, String dayOfBirth, String phone,
			String sex) {
		Connection con = DbConnection.getConnect();
		String sql = "UPDATE user SET dayOfBirth = ? , phone = ? , sex = ?  WHERE email = ?";
		PreparedStatement pre;
		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, dayOfBirth);
			pre.setString(2, phone);
			pre.setString(3, sex);
			pre.setString(4, email);
			pre.executeUpdate();
			pre.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static ArrayList<Communication> getListCommunication(int type) {

		ArrayList<Communication> ListCommunication = new ArrayList<>();
		try {

			Connection connection = DbConnection.getConnect();
			String sql = "select * from communication WHERE type = ?";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, type);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				String title = resultSet.getString("title");
				String link = resultSet.getString("link");
				long timeContent = resultSet.getLong("timeContent");
				String email = resultSet.getString("user_email");
				Communication com = new Communication(title, link, timeContent,
						type, email);
				ListCommunication.add(com);
			}
			preparedStatement.close();
			connection.close();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return ListCommunication;

	}

	public static Communication getCommunication(long timeContent) {
		Communication con = null;
		try {

			Connection connection = DbConnection.getConnect();
			String sql = "SELECT * FROM communication WHERE timeContent = ?";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setLong(1, timeContent);
			ResultSet rs = preparedStatement.executeQuery();

			while (rs.next()) {
				String link = rs.getString("link");
				String title = rs.getString("title");
				int type = rs.getInt("type");
				String email = rs.getString("user_email");
				con = new Communication(title, link, timeContent, type, email);

			}
			rs.close();
			preparedStatement.close();
			connection.close();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return con;

	}

	public static ArrayList<Communication> getHotCommunication() {
		ArrayList<Communication> Listcon = new ArrayList<>();
		try {

			Connection connection = DbConnection.getConnect();
			String sql = "select * from communication order by timeContent desc limit 5";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {

				long timeContent = rs.getLong("timeContent");
				String title = rs.getString("title");
				String link = rs.getString("link");
				int type = rs.getInt("type");
				String email = rs.getString("user_email");

				Communication con = new Communication(title, link, timeContent,
						type, email);
				Listcon.add(con);
			}
			rs.close();
			preparedStatement.close();
			connection.close();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return Listcon;

	}

	public static void addCardUser(String idSeriCard, long time, String date,
			String email) {
		try {

			Connection connection = DbConnection.getConnect();
			String sql = "INSERT INTO carduser VALUES(?,?,?,?)";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setLong(1, time);
			preparedStatement.setString(2, idSeriCard);
			preparedStatement.setString(3, email);
			preparedStatement.setString(4, date);
			preparedStatement.executeUpdate();
			preparedStatement.close();
			connection.close();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	public static int getLevel(String email , String role_id) {
		int level = 0;
		try {

			Connection connection = DbConnection.getConnect();
			String sql = "select * from task where user_email= ? AND role_id = ?";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setString(1, email);
			preparedStatement.setString(2, role_id);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				level = rs.getInt("level");
			}
			rs.close();
			preparedStatement.close();
			connection.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return level;

	}

	public static void main(String[] args) {

		// User u = new User("Hoang@zxz", "KaiOshin", "121212", "vo doi",
		// "name", "2.1060", "sda", "asd");
	}

}
